package com.GPT.controller;

import com.GPT.common.Result;
import com.GPT.common.UserId;
import com.GPT.domain.Newuser;
import com.GPT.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class InviteController {

    @Autowired
    private UserService userService;

    @GetMapping("/generate-invite-code")
    public Map<String, String> generateInviteCode(HttpServletRequest request) {
        Long userId = new UserId().getUserId(request);
        Newuser byId = userService.getById(userId);
        // 生成随机邀请码
        String inviteCode = byId.getCode();
        // 生成分享链接
        String link = "https://www.baidu.com/";
        Map<String, String> response = new HashMap<>();
        response.put("inviteCode", inviteCode);
        response.put("link", link);
        return response;
    }

    @GetMapping("/addToken")
    public Result validateInviteCode(HttpServletRequest request,@RequestParam String code) {
        if (code == null || code.length() != 4) {
            return Result.error("邀请码不能为空且必须为四位");
        }
        Result result = userService.validateInviteCode(request,code);
        return result;
    }

}

